#author :BMooS &  Canyue
#---------------------------
import re
import requests
import os
import time
import shutil
#----------------------------------
print('author:BMooS & Canyue'+'  '+'v1.0')
print('这是内部使用简陋简易简单喜马拉雅音频获取器')
time.sleep(1)
print('只限于喜马拉雅FM免费经典榜TOP100')
time.sleep(1)
print('为保险起见')
time.sleep(1)
print('不要转发和进行其他蜜汁操作,仅供内部学习')
time.sleep(1)
input('任意键选择开始')
#-----------------------------------------
print('以下是你要选择的合集：')
print('----------------------------------')
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'}
html = requests.get('https://www.ximalaya.com/top/',headers = headers)
html.encoding = 'utf-8'
#--------------------------------------
name = re.findall(r'<div class="title kus">(.*?)</div>',html.text,re.S)
i = 1
for a in name :
    print(str(i) + "." + str(a))
    i += 1
cut_url = re.findall(r'kus"><a href="(.*?)"><div class="rrc-sort-num kus">',html.text,re.S)
number = input("选择你想下载的合集编号：")
use_cut_url = cut_url[int(number)-1]
#------------------------------------------------------

html_1 = requests.get('https://www.ximalaya.com' + str(use_cut_url),headers = headers)
name_1 = re.findall(r'<html><head><title data-react-helmet="true">(.*?)</title>',html_1.text,re.S)

print(name_1[0])

page = re.findall(r'<span class="tthf">(.*?)</span>',html_1.text,re.S)

list_all = []     #所有音频url末尾id
list_page = []    #页数
list_name = []    #每个音频名字

#-------------------------------------

if page == []:
    page_1 = 1
else:
    page_1=int(page[-1])

u = 1
while u <= page_1:
    list_page.append(str(u))
    u += 1

print('本合集共有'+ str(page_1) +'页')

time.sleep(5)
for n in list_page:
    print('第'+str(n)+"页的内容：")
    time.sleep(3)
    if n == str(1) :
        html_1_1 = requests.get('https://www.ximalaya.com' + str(use_cut_url),headers = headers)
        name_1_1 = re.findall(r'rC5T"><a title="(.*?)" href="',html_1_1.text,re.S)
        url_1 = re.findall(r'" href="(%s)(.*?)">'%use_cut_url,html_1_1.text,re.S)
        for b in name_1_1:
            list_name.append(b)
        r = 0
        for g in url_1:
            if len(str(url_1[r][1])) >= 4 :
                list_all.append(url_1[r][1])
                r += 1
        o = 1
        for u in name_1_1:
            print(str(o)+'.  '+u)
            o += 1
    else:
        html_1_1 = requests.get('https://www.ximalaya.com' + str(use_cut_url) +'p'+ str(n) + '/',headers = headers)
        name_1_1 = re.findall(r'rC5T"><a title="(.*?)" href="',html_1_1.text,re.S)
        url_1 = re.findall(r'" href="(%s)(.*?)">'%use_cut_url,html_1_1.text,re.S)
        for b in name_1_1:
            list_name.append(b)
        r = 0
        for g in url_1:
            if len(str(url_1[r][1])) >= 4 :
                list_all.append(url_1[r][1])
                r += 1
        o = 1
        for u in name_1_1:
            print(str(o)+'.  '+u)
            o += 1
s = input('批量下载或是单个下载 1或2：')

#-------------------------------------
# print(len(list_all))
# print(len(list_name))
if len(list_all)==len(list_name):
    print('可以进行')
else:
    print('含有异常')
    time.sleep(3)
    print('继续使用')
#--------------------------------------------------
homedir = os.getcwd()   #获取项目当前路径
if os.path.exists(homedir+'\pyp'):
    shutil.rmt ree(homedir+'\pyp')
    print('删除本路径下原有文件夹并创建新文件夹')
    time.sleep(1)
    os.mkdir(homedir+'\pyp')
else:
    print('在本路径下创建新文件夹')
    os.mkdir(homedir+'\pyp')

#----------------------------------------------

if s == str(2) :
    c = input('选择页码数')
    h = input('选择哪音频编号')
    print('正在下载...')
    html_1_1_1 = requests.get("https://www.ximalaya.com/revision/play/tracks?trackIds=" + str(list_all[(int(c)-1)*30+int(h)-1]),headers = headers)
    url_end = re.findall(r'"src":"(.*?)"',html_1_1_1.text,re.S)
    url_k = '.'.join(url_end)
    url_v = requests.get(url_k,headers = headers)
    fp = open('pyp\\' + '喜马拉雅' + '.m4a','wb')  #在当前文件夹下创建m4a格式的文件
    url_s = '.'.join(url_end)
    fp.write(url_v.content)  #将音频写入到当前文件夹中
    print('success')
    time.sleep(5)
    fp.close()


#-----------------------------------------------

if s == str(1) :
    c = input('选择页码数')
    print('正在下载...')
    y = 1
    for w in range(30):
        html_1_1_1 = requests.get("https://www.ximalaya.com/revision/play/tracks?trackIds=" + str(list_all[(int(c)-1)*30-1+y]),headers = headers)
        url_end = re.findall(r'"src":"(.*?)"',html_1_1_1.text,re.S)
        url_k = '.'.join(url_end)
        url_v = requests.get(url_k,headers = headers)
        fp = open('pyp\\' + str(y) + '.m4a','wb')  #在当前文件夹下创建m4a格式的文件
        url_s = '.'.join(url_end)
        fp.write(url_v.content)  #将音频写入到当前文件夹中
        print('success') 
        fp.close()
        y += 1